import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:flutter_widget/provider/count_provider.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class ProviderPage extends StatefulWidget {
  String name;
  ProviderPage({Key? key, required this.name}) : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  _ProviderPageState createState() => _ProviderPageState();
}

class _ProviderPageState extends State<ProviderPage> {
  @override
  Widget build(BuildContext context) {
    final String count =
        Provider.of<CountProvider>(context).count.toString(); //获取state的值
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CircleAvatar(
              backgroundColor: Theme.of(context).primaryColor,
              child: Text(count),
              foregroundColor: Colors.white,
            ),
          ],
        ),
      ),
      persistentFooterButtons: [
        // ignore: deprecated_member_use
        ElevatedButton(
          child: const Icon(
            Icons.add,
            color: Colors.white,
          ),
          onPressed: () {
            context.read<CountProvider>().increase(); // 修改state的值
          },
        ),
        // ignore: deprecated_member_use
        ElevatedButton(
          child: const Icon(
            Icons.remove,
            color: Colors.white,
          ),
          onPressed: () {
            context.read<CountProvider>().decrease(); // 修改state的值
          },
        ),
      ],
    );
  }
}
